package com.lh0811.forge.pserv.uacs.server.adapter.controller;

import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.annotation.HasUrl;
import com.lh0811.forge.pserv.uacs.api.param.*;
import com.lh0811.forge.pserv.uacs.api.vo.UacsPlateResQueryVo;
import com.lh0811.forge.pserv.uacs.api.vo.UacsPlateRoleQueryVo;
import com.lh0811.forge.pserv.uacs.server.service.PlateRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "权限中心-平台角色管理")
@RestController
@RequestMapping("/forge/plate/v1/uacs/plate_role")
public class OpmsPlateRoleController {

    @Resource
    private PlateRoleService plateRoleService;


    // 新建角色信息
    @HasUrl
    @PutMapping("/create")
    @Operation(summary = "新建角色信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateRoleCreate(@RequestBody @Valid UacsPlateRoleCreateParam param) throws Exception {
        plateRoleService.plateRoleCreate(param);
        return ServerResponse.createBySuccess();
    }

    // 删除角色信息
    @HasUrl
    @DeleteMapping("/delete/{roleId}")
    @Operation(summary = "删除角色信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateRoleDelete(@PathVariable(value = "roleId") Long roleId) throws Exception {
        plateRoleService.plateRoleDelete(roleId);
        return ServerResponse.createBySuccess();
    }

    // 更新角色信息
    @HasUrl
    @PostMapping("/update")
    @Operation(summary = "更新角色信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateRoleUpdate(@RequestBody @Valid UacsPlateRoleUpdateParam param) throws Exception {
        plateRoleService.plateRoleUpdate(param);
        return ServerResponse.createBySuccess();
    }

    // 查询角色信息(分页)
    @HasUrl
    @GetMapping("/query/page")
    @Operation(summary = "查询角色信息（分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<PageInfo<UacsPlateRoleQueryVo>> plateRoleQueryPage(@ParameterObject @Valid UacsPlateRoleQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", plateRoleService.plateRoleQueryPage(param));
    }

    // 查询角色信息(不分页)
    @HasUrl
    @GetMapping("/query/list")
    @Operation(summary = "查询角色信息（不分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsPlateRoleQueryVo>> plateRoleQueryList(@ParameterObject @Valid UacsPlateRoleQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", plateRoleService.plateRoleQueryList(param));
    }

    // 保存角色和资源关系
    @HasUrl
    @PostMapping("/res_ref/save")
    @Operation(summary = "保存角色和资源关系", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateRoleResRefSave(@RequestBody @Valid UacsPlateRoleResRefSaveParam param) throws Exception {
        plateRoleService.plateRoleResRefSave(param);
        return ServerResponse.createBySuccess();
    }

    // 获取角色绑定的资源列表
    @HasUrl
    @GetMapping("/building/res_list/{roleId}")
    @Operation(summary = "获取角色绑定的资源列表", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsPlateResQueryVo>> getRoleBuildingResList(@PathVariable(value = "roleId") Long roleId) throws Exception {
        return ServerResponse.createBySuccess(plateRoleService.getRoleBuildingResList(roleId));
    }

}
